package com.etl.mh.borrow;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.etl.borrow.model.Borrow;
import com.etl.borrow.service.IBorrowService;
import com.xr.dto.ResultDto;
import com.xr.util.Utils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Map;

/**
 * <p>
 * 借款项目 控制器
 * </p>
 *
 * @author Arnold Yang
 * @since 2018-03-24
 */
@Controller
@RequestMapping("/borrow")
public class BorrowController {

  @Resource
  private IBorrowService borrowService;

  /**
   * 可投资标的列表
   * @param start 第几页，默认1
   * @param length 每页多少行，默认10
   * @param status 标的状态
   * @return
   * @throws Exception
   */
  @ResponseBody
  @RequestMapping("/list")
  public Map borrowList(Integer start, Integer length, Integer status) throws Exception {

    EntityWrapper<Borrow> wrapper = new EntityWrapper<Borrow>();
    if(status != null){
      wrapper.where("status = {0}", status);
    }
    wrapper.orderBy("create_time", false);

    long totalRecords = borrowService.selectCount(wrapper);

    return Utils.newHashMap(
      "length", length,
      "iTotalRecords", totalRecords,
      "iTotalDisplayRecords", totalRecords,
      "data", borrowService.selectPage(new Page<Borrow>((start/length)+1, length), wrapper).getRecords()
    );
  }

  /**
   * 删除借款标的
   *
   * @param borrowId 标的id
   * @return
   * @throws Exception
   */
  @ResponseBody
  @RequestMapping("/del")
  public ResultDto delBorrow(Long borrowId) throws Exception {
    borrowService.deleteById(borrowId);
    return ResultDto.success("删除成功");
  }

}
